<template>
	<view class="main-page" style="background-color: white">
		<view class="row-container width-100p" style="height: 120rpx;">
			<image src="../../static/ic_location.png"
			class="location-ic"></image>
			<text class="font-333-32 font-weight-600">
				{{cityName}}</text>
				
			<view class="search-container">
				<image class="search-ic" src="../../static/ic_search.png"></image>
				<input 
					v-model="keyword"
					class="input"
					placeholder="请输入您的位置"
					placeholder-style="font-999-32"
					confirm-type="search"
					@confirm="clickToSearch"
				/>
			</view>
		</view>
		<view class="current-container">
			<text class="font-333-32 ml-30">当前定位：{{addressStr}}</text>
			<view class="row-container mr-30" style="flex-shrink: 0;" @click="getLocation">
				<image class="re-location-ic" src="../../static/ic_relocation.png"></image>
				<text class="font-333-32">重新定位</text>
			</view>
		</view>
		<view class="divider-20"></view>
		<view class="row-container ml-30" style="margin-top: 34rpx;">
			<image src="../../static/ic_location_grey.png" class="more"></image>
			<text class="font-999-28" style="margin-left: 4rpx;">附近地址</text>
		</view>
		<view class="column-container width-100p">
			<view class="column-container width-100p" @click="changeAddress(item)"
			 v-for="(item , index) in addressList" :key="index">
				<text class="single-address">{{item.name}}</text>
				<view class="list-divider"></view>
			</view>
		</view>
		
	</view>
</template>

<script>
	import AmapFile from '../../assets/amap-wx.130.js'
	import config from '../../utils/config.js'
	
	export default {
		data() {
			return {
				keyword:'',
				addressList:[],
				latitude:'',
				longitude:'',
				cityName:'',
				addressStr:'' ,
				page:1,
				
				aMap:null ,
			}
		},
		
		onLoad() {
			this.aMap = new AmapFile.AMapWX({key:config.amapKey})
			this.getLocation()
		},
		
		methods: {
			getLocation(){
				let that = this
				uni.authorize({
					scope:'scope.userLocation',
					success() {
						uni.getLocation({
							type:"gcj02",
							success(locationRes) {
								console.log('location--------',JSON.stringify(locationRes))
								that.longitude = locationRes.longitude
								that.latitude = locationRes.latitude
								
								that.handleFormatAddress()
							},
							fail(e) {
								console.log("location---获取经纬度失败---")
							},
							complete() {
								
							}
							
						})
					}
				})
			},
			
			handleFormatAddress(){
				let that = this
				that.aMap.getRegeo({
					location:that.longitude + "," + that.latitude,
					success(aMapRes){
						console.log("解析地址成功---", aMapRes);
						if (aMapRes.length > 0){
							that.addressStr = aMapRes[0].desc
							that.cityName = aMapRes[0].regeocodeData.addressComponent.city
							that.doSearchAddress(that.addressStr)
						}
					},
					failed(aMapErr){
						console.log('amap--failed---' , aMapErr)
					}
				})
			},
			
			clickToSearch(){
				if (this.keyword){
					this.doSearchAddress()
				}
			},
			
			doSearchAddress(searchWord){
				console.log("doSearchAddress----" , searchWord)
				let that = this
				this.aMap.getInputtips({
					keywords:searchWord ? searchWord : that.keyword,
					city:that.cityName,
					citylimit:true,
					location:that.longitude + "," + that.latitude,
					success(res){
						console.log('search---result---' , res)
						that.addressList = res.tips
					},
					failed(err){
						console.log("amap--search--failed--" , err)
					}
					
				})
			},
			
			//修改地址
			changeAddress(item){
				let addressItem = {}
				console.log('changeAddress----' + JSON.stringify(item))
				this.$set(addressItem , "addressStr" , item.name)
				this.$set(addressItem , 'latitude' , item.location.split(',')[1])
				this.$set(addressItem , 'longitude' , item.location.split(',')[0])
				uni.$emit("changeAddress" , addressItem)
				uni.navigateBack({delta:1})
			}
		}
	}
</script>

<style lang="scss">
	.location-ic{
		width: 40rpx;
		height: 40rpx;
		margin-right: 4rpx;
		margin-left: 30rpx;
	}
	.search-container{
		display: flex;
		flex-direction: row;
		flex-grow: 1;
		height: 76rpx;
		margin-left: 56rpx;
		margin-right: 30rpx;
		align-items: center;
		background-color: #F5F5F5;
		border-radius: 10rpx;
		
		.search-ic{
			width: 30rpx;
			height: 30rpx;
			margin-left: 20rpx;
			margin-right: 20rpx;
			flex-shrink: 0;
		}
		
		.input{
			flex-grow: 1;
			height: 76rpx;
			margin-right: 30rpx;
			color: #333333;
			font-size: 32rpx;
		}
	}
	.current-container{
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;
		width: 100%;
		height: 90rpx;
		
		.re-location-ic{
			width: 34rpx;
			height: 34rpx;
			margin-right: 8rpx;
		}
	}
	
	.single-address{
		width: calc(100% - 128rpx);
		padding: 34rpx 64rpx;
		color: #333333;
		font-size: 30rpx;
		font-weight: 600;
	}
	.list-divider{
		width: calc(100% - 98rpx);
		height: 1rpx;
		margin-left: 68rpx;
		background-color: #EDEDED;
	}
	
	
</style>
